﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using Excel = Microsoft.Office.Interop.Excel;
using HtmlAgilityPack;

namespace WebScraping
{
    public partial class frmVINscrap : Form
    {
        public frmVINscrap()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            DisplayOpenFileDialog();
        }
        public void DisplayOpenFileDialog()
        {
            System.Windows.Forms.OpenFileDialog openFile = new System.Windows.Forms.OpenFileDialog();
            openFile.DefaultExt = "";
            openFile.Filter = "Text Files(*.txt)|*.txt|All files (*.*)|*.*";
            openFile.ShowDialog();
            if (openFile.FileNames.Length > 0)
            {
                string filename = null;
                foreach (string filename_loopVariable in openFile.FileNames)
                {
                    filename = filename_loopVariable;
                    //Load pictureBox
                    txtFile.Text = filename;
                }
            }
        }

        private void btDoIt_Click(object sender, EventArgs e)
        {
            if (txtFile.Text == "")
                return;
            //bắt đầu đọc file text
            // Read each line of the file into a string array. Each element 
            // of the array is one line of the file. 
            string[] lines = System.IO.File.ReadAllLines(txtFile.Text );

            if (lines.Length == 0)
            {
                MessageBox.Show("File has no info!", "Thông báo");
                return;
            }
            //mở file Excel
            Excel.Application xlApp;
            Excel.Workbook xlBook;
            Excel.Worksheet xlSheet;

            xlApp = new Excel.Application();
            xlBook = xlApp.Workbooks.Open(Application.StartupPath + "\\VIN.xlsx");
            xlSheet = xlBook.Sheets[1];
            xlApp.Visible = true;
            //điều chỉnh lại Form Height
            this.Height = 193;
            //hiển thị progress bar
            int max = lines.Length  - 1;
            this.myProgress.Maximum = max;
            string strValues = "";
            // Display the file contents by using a foreach loop.
            for (int i = 0; i < lines.Length; i++)
            {
                string line = lines[i].Trim ();
                strValues = "Scraping at VIN:" + line;
                myProgress.Value = i;
                lblThongBao.Text = strValues;
                Application.DoEvents();
                //thực hiện lấy thông tin
                LayThongTin(xlSheet, line, i + 2);
            }
            this.Height = 116;
        }

        private void LayThongTin(Excel.Worksheet sheet, string VIN, int row) { 
            //CScraping objCtrl = new CScraping ();
            //string web = objCtrl.GetWebContent("http://www.toyodiy.com/parts/q?vin=" + VIN);
            //HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
            //doc.LoadHtml(web);
            var client = new MyWebClient();
            HtmlAgilityPack.HtmlDocument doc = client.GetPage("http://www.toyodiy.com/parts/q?vin=" + VIN);
            //phân tích
            string market = doc.DocumentNode.SelectSingleNode("//div[@id='wM']/a").InnerText ;
            string year = doc.DocumentNode.SelectSingleNode("//div[@id='wYR']/a").InnerText;
            string make = doc.DocumentNode.SelectSingleNode("//div[@id='wMK']/a").InnerText;
            string model = doc.DocumentNode.SelectSingleNode("//div[@id='wMD']/a").InnerText;
            string frame = doc.DocumentNode.SelectSingleNode("//div[@id='wFR']/a").InnerText;

            HtmlNode tableCharacter = doc.DocumentNode.SelectSingleNode("//div[@class='opts']/table");
            HtmlNodeCollection rows = tableCharacter .SelectNodes ("tr");
            string Vmodel= rows[0].SelectNodes ("td")[1].InnerText +" " +rows[0].SelectNodes ("td")[2].InnerText ;
            string grade =rows[1].SelectNodes ("td")[1].InnerText +" " +rows[1].SelectNodes ("td")[2].InnerText;
            string des= rows[2].SelectNodes ("td")[1].InnerText +" " +rows[2].SelectNodes ("td")[2].InnerText;
            string engine = rows[3].SelectNodes("td")[1].InnerText + " " + rows[3].SelectNodes("td")[2].InnerText;
            string gear = rows[4].SelectNodes("td")[1].InnerText + " " + rows[4].SelectNodes("td")[2].InnerText;
            string seating = rows[5].SelectNodes("td")[1].InnerText + " " + rows[5].SelectNodes("td")[2].InnerText;
            string color = rows[6].SelectNodes("td")[1].InnerText + " " + rows[6].SelectNodes("td")[2].InnerText;
            string trim = rows[7].SelectNodes("td")[1].InnerText + " " + rows[7].SelectNodes("td")[2].InnerText;
            string url = "http://www.toyodiy.com/parts/" + doc.DocumentNode.SelectSingleNode("//a[@title='click to select']").Attributes["href"].Value;
        
            //insert vào excel
            sheet.Cells[row, 1] = VIN;
            sheet.Cells[row, 2] = market;
            sheet.Cells[row, 3] = year;
            sheet.Cells[row, 4] = make;
            sheet.Cells[row, 5] = model;
            sheet.Cells[row, 6] = frame ;
            sheet.Cells[row, 7] = Vmodel ;
            sheet.Cells[row, 8] = grade ;
            sheet.Cells[row, 9] = des;
            sheet.Cells[row, 10] = engine ;
            sheet.Cells[row, 11] = gear ;
            sheet.Cells[row, 12] = seating ;
            sheet.Cells[row, 13] = color;
            sheet.Cells[row, 14] = trim;
            sheet.Cells[row, 15] = url;
        }

        private void frmVINscrap_Load(object sender, EventArgs e)
        {
            this.Height = 116;
        }
    }
}
